<?php


namespace app\common\model;

use think\model\concern\SoftDelete;

class ShopsSeat extends Base
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'ddt_shops_seat';
    // 主键ID
    protected $pk = 'seat_id';

	// 要使用软删除功能，需要引入SoftDelete
	use SoftDelete;
	//deleteTime属性用于定义你的软删除标记字段
	//ThinkPHP的软删除功能使用时间戳类型（数据表默认值为Null），用于记录数据的删除时间。
	protected $deleteTime = 'is_del';
	//defaultSoftDelete属性来定义软删除字段的默认值
	protected $defaultSoftDelete = 1;

    // 获取分页列表
    function get_list_page($cond,$item = '*',$orderby = 'add_time desc,seat_id desc',$limit = 10)
    {
        $where = null;
        if(!empty($cond['shop_name'])){
            $where[] = ['shop_name','like',"%".$cond['shop_name']."%"];
        }

        if(isset($cond['shop_id'])){
            $where[] = ['shop_id', '=',$cond['shop_id']];
        }

        if(!empty($cond['approved'])){
            $where[] = ['approved', '=',$cond['approved']];
        }

        if(!empty($cond['state'])){
            $where[] = ['state', '=',$cond['state']];
        }
        $where[] = ['is_del','=', 1];

        $rs = $this
            ->field($item)
            ->where($where)
            ->order($orderby)
            ->paginate($limit);
        return $rs;
    }

    //新增、编辑(后台操作)
    function edit($params, $id=0){
        if($id>0){
            // 更新条件
            $where = ["{$this->pk}" => $id];
			$params['update_time'] = time();
            $rs = $this->allowField(true)->save($params, $where);
        }else{
            // 更新数据
			$params['add_time'] = time();
            $this->allowField(true)->save($params);
            $rs = $this->pk;
        }
        if ($rs === false) {
            $this->setError(-1, '操作失败');
            return false;
        }
        return $rs;
    }

	function Del($id)
	{
		ShopsSeat::destroy($id);
		$res = $this->delete();
		return $res;
	}

	function getseatinfo($cond,$item = 'st.*'){
        $where = [];
        if($cond['seat_id']){
            $where[] = ['seat_id','=',$cond['seat_id']];
        }
        $data = $this
            ->alias('st')
            ->leftJoin("ddt_shops s","st.shop_id = s.shop_id")
            ->where($where)
            ->field($item)
            ->find();
        return $data;
    }
}